package edu.guigu.uiweixindemo;

import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

import edu.guigu.uiweixindemo.contact.ContactFragment;
import edu.guigu.uiweixindemo.friend.FriendsFragment;
import edu.guigu.uiweixindemo.me.MeFragment;
import edu.guigu.uiweixindemo.talk.TalkFragment;

public class MainActivity extends AppCompatActivity {

    private ViewPager2 viewPager2;
    private TabLayout tabLayout;
    private MainAdapter adapter;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        // 数据初始化
        initData();
        // 视图初始化
        initView();
    }

    private void initData() {
        fragments = new ArrayList<>();
        fragments.add(TalkFragment.newInstance("聊天", R.drawable.ic_talk));
        fragments.add(ContactFragment.newInstance("通讯录", R.drawable.ic_contact));
        fragments.add(FriendsFragment.newInstance("朋友圈", R.drawable.ic_friend));
        fragments.add(MeFragment.newInstance("我的", R.drawable.ic_me));
        adapter = new MainAdapter(this);
        adapter.setDataList(fragments);
    }

    private void initView() {
        viewPager2 = findViewById(R.id.viewpage2);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager2.setAdapter(adapter);
        new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
            tab.setText(fragments.get(position).getArguments().getString("param1"));
            tab.setIcon(fragments.get(position).getArguments().getInt("param2"));
        }).attach();
    }
}